import { get, post, put, del } from '@/utils/request'

// 菜单项接口定义
export interface MenuItem {
  id: number
  name: string
  path: string
  component?: string
  parent_id?: string
  meta: {
    roles?: string
    requires_auth?: boolean
    icon?: string
    locale?: string
    hideIn_menu?: boolean
    hide_childrenIn_menu?: boolean
    active_menu?: string
    order?: number
    no_affix?: boolean
    ignore_cache?: boolean
    property1?: string
    property2?: string
  }
  createTime?: string
  updateTime?: string
  children?: MenuItem[]
}

// 创建菜单请求参数
export interface CreateMenuRequest {
  name: string
  path: string
  component?: string
  parent_id?: string
  meta: {
    roles?: string
    requires_auth?: boolean
    icon?: string
    locale?: string
    hideIn_menu?: boolean
    hide_childrenIn_menu?: boolean
    active_menu?: string
    order?: number
    no_affix?: boolean
    ignore_cache?: boolean
    property1?: string
    property2?: string
  }
}

// 更新菜单请求参数
export interface UpdateMenuRequest extends CreateMenuRequest {
  id: number
}

// 获取菜单列表
export function getMenuList(): Promise<MenuItem[]> {
  return get<MenuItem[]>('/api/sys/menu/all')
}

// 获取菜单树形结构
export function getMenuTree(): Promise<MenuItem[]> {
  return get<MenuItem[]>('/api/user/menu/tree')
}

// 添加菜单
export function addMenu(data: CreateMenuRequest): Promise<MenuItem> {
  return post<MenuItem>('/api/sys/menu/add', data)
}

// 更新菜单
export function updateMenu(data: UpdateMenuRequest): Promise<MenuItem> {
  return put<MenuItem>('/api/user/menu/update', data)
}

// 删除菜单
export function deleteMenu(id: number): Promise<void> {
  return del(`/api/user/menu/delete/${id}`)
}

// 获取菜单详情
export function getMenuDetail(id: number): Promise<MenuItem> {
  return get<MenuItem>(`/api/user/menu/detail/${id}`)
}

// 批量删除菜单
export function batchDeleteMenu(ids: number[]): Promise<void> {
  return del('/api/user/menu/batch-delete', { data: { ids } })
}

